package com.settlement.system.service.impl;

import com.settlement.system.common.util.StringUtils;
import com.settlement.system.model.entity.PddBill;
import com.settlement.system.mapper.PddBillMapper;
import com.settlement.system.model.entity.SysStore;
import com.settlement.system.service.PddBillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.settlement.system.service.SysStoreService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.PddBillQuery;
import com.settlement.system.model.form.PddBillForm;
import cn.hutool.core.bean.BeanUtil;
import com.settlement.system.model.vo.PddBillImportVo;

/**
 * <p>
 * 拼多多账单 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-04-13
 */
@Service
@RequiredArgsConstructor
public class PddBillServiceImpl extends ServiceImpl<PddBillMapper, PddBill> implements PddBillService {

    private final SysStoreService sysStoreService;

    @Override
    public Page<PddBill> getPage(PddBillQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        String tableName = "pdd_bill_%s".formatted(queryParams.getBillMonth());
        queryParams.setTableName(tableName);
        if (!StringUtils.hasText(this.baseMapper.existTable(tableName))) {
            return new Page<>(pageNum, pageSize);
        }
        Page<PddBill> page = new Page<>(pageNum, pageSize);
        this.baseMapper.getPage(page, queryParams);
        SysStore sysStore;
        for (PddBill record : page.getRecords()) {
            sysStore = sysStoreService.getById(record.getStoreId());
            if (sysStore != null) {
                record.setStoreName(sysStore.getBillSystemStoreName());
            }
        }
        return page;
    }

     @Override
     public boolean savePddBill(PddBillForm pddBillForm) {
         PddBill pddBill = new PddBill();
         BeanUtil.copyProperties(pddBillForm, pddBill);
         pddBill.setCreateTime(null);
         return this.saveOrUpdate(pddBill);
     }

     @Override
     public PddBillForm getPddBillFormData(Long id) {
         PddBill pddBill = this.getById(id);
         PddBillForm pddBillForm = new PddBillForm();
         BeanUtil.copyProperties(pddBill, pddBillForm);
         return pddBillForm;
     }

     @Override
     public boolean deletePddBill(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<PddBillImportVo> voList)  {
        List<PddBill> pddBillList = new ArrayList<>();
        for (PddBillImportVo importVo : voList) {
            PddBill pddBill = new PddBill();
            BeanUtil.copyProperties(importVo, pddBill);
            pddBill.cutString();
            pddBillList.add(pddBill);
        }
        this.saveOrUpdateBatch(pddBillList);
    }

    @Override
    public List<PddBillImportVo> findByIds(String idsStr) {
        if (StrUtil.isBlank(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<PddBill> pddBillList = this.getBaseMapper().selectBatchIds(ids);
        List<PddBillImportVo> voList = new ArrayList<>();
        for (PddBill pddBill : pddBillList) {
            PddBillImportVo vo = new PddBillImportVo();
            BeanUtil.copyProperties(pddBill, vo);
            voList.add(vo);
        }
        return voList;
    }
}
